package net.lab1024.sa.admin.module.business.place.service;

import java.util.List;

import net.lab1024.sa.admin.module.business.category.domain.form.CategoryTreeQueryForm;
import net.lab1024.sa.admin.module.business.category.domain.vo.CategoryTreeVO;
import net.lab1024.sa.admin.module.business.category.manager.CategoryCacheManager;
import net.lab1024.sa.admin.module.business.place.dao.PlaceDao;
import net.lab1024.sa.admin.module.business.place.domain.entity.PlaceEntity;
import net.lab1024.sa.admin.module.business.place.domain.form.PlaceAddForm;
import net.lab1024.sa.admin.module.business.place.domain.form.PlaceQueryForm;
import net.lab1024.sa.admin.module.business.place.domain.form.PlaceTreeQueryForm;
import net.lab1024.sa.admin.module.business.place.domain.form.PlaceUpdateForm;
import net.lab1024.sa.admin.module.business.place.domain.vo.PlaceTreeVO;
import net.lab1024.sa.admin.module.business.place.domain.vo.PlaceVO;
import net.lab1024.sa.admin.module.business.place.manager.PlaceCacheManager;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 地址 Service
 *
 * @Author syf
 * @Date 2025-05-21 16:29:55
 * @Copyright 畅想科技
 */

@Service
public class PlaceService {

    @Resource
    private PlaceDao placeDao;

    @Resource
    private PlaceCacheManager placeCacheManager;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<PlaceVO> queryPage(PlaceQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<PlaceVO> list = placeDao.queryPage(page, queryForm);
        PageResult<PlaceVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(PlaceAddForm addForm) {
        PlaceEntity placeEntity = SmartBeanUtil.copy(addForm, PlaceEntity.class);
        placeDao.insert(placeEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(PlaceUpdateForm updateForm) {
        PlaceEntity placeEntity = SmartBeanUtil.copy(updateForm, PlaceEntity.class);
        placeDao.updateById(placeEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }
        placeDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }
    public ResponseDTO<List<PlaceTreeVO>> queryTree(PlaceQueryForm queryForm) {
        if (null == queryForm.getParentId()) {
            queryForm.setParentId(NumberUtils.LONG_ZERO);
        }
        List<PlaceTreeVO> treeList = placeCacheManager.queryPlaceTree(queryForm.getParentId());
        return ResponseDTO.ok(treeList);
    }
    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long placeId) {
        if (null == placeId){
            return ResponseDTO.ok();
        }

        placeDao.deleteById(placeId);
        return ResponseDTO.ok();
    }
}
